import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.YdmVerify;

import java.time.Duration;

/**
 * @author:CoolTiger
 * @description: TODO
 * @date:2024/03/06/23:19:28
 **/

public class Tests {
    @Test
    public void test(){
        System.setProperty("webdriver.chrome.driver","G:\\Desktop\\chromedriver-win64\\chromedriver.exe");   //设置chrome驱动程序的路径
        // 1.创建webdriver驱动
        ChromeOptions options = new ChromeOptions();
        //options.setBinary("C:\\Users\\19760\\Downloads\\chrome-win64\\chrome.exe");
        WebDriver driver = new ChromeDriver(options);
        // 2.打开百度首页
        driver.get("https://www.baidu.com");
        // 3.获取输入框，输入selenium
        driver.findElement(By.id("kw")).sendKeys("selenium");
        // 4.获取“百度一下”按钮，进行搜索
        driver.findElement(By.id("su")).click();
        System.out.println( driver.getTitle());
        // 5.退出浏览器
        // driver.quit();
    }

    /**
     * 登录苏通15测试
     */
    @Test
    public void login(){
        System.setProperty("webdriver.chrome.driver","G:\\Desktop\\chromedriver-win64\\chromedriver.exe");   //设置chrome驱动程序的路径
        // 1.创建webdriver驱动
        ChromeOptions options = new ChromeOptions();
        WebDriver driver = new ChromeDriver(options);
        // 打开登录页面
        driver.get("http://sutongiot.com:17101/login?redirect=%2Findex");

        try {
            // 填写账号
            WebElement usernameInput = driver.findElement(By.cssSelector("input[type='text']")); // 通过CSS选择器找到用户名输入框
            usernameInput.sendKeys("admin");
            // 填写密码
            WebElement passwordInput = driver.findElement(By.cssSelector("input[type='password']")); // 通过CSS选择器找到密码输入框
            passwordInput.sendKeys("admin123");
            // 获取验证码base64
            WebElement imgElement = driver.findElement(By.cssSelector(".login-code-img"));
            String imageUrl = imgElement.getAttribute("src");
            // 截取正确的base64字符串
            String base64Url = imageUrl.substring(imageUrl.indexOf(',') + 1);
            System.out.println("Image URL: " + base64Url);
            //解析验证码结果
            String image_content = YdmVerify.commonVerify(base64Url.trim());
            JSONObject result = JSON.parseObject(image_content);
            String code = "";
            if (result.getInteger("code").equals(10000)){
                code = result.getJSONObject("data").getString("data");
            }
            System.out.println("验证码: " + code);
            // 填写验证码
            WebElement elInput = driver.findElement(By.xpath("//input[@placeholder='验证码']"));
            elInput.sendKeys(code);
            // 等待登录按钮可点击，创建 WebDriverWait 对象，设置等待时间为2秒
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
            WebElement elButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[@type='button']")));
            // 点击 登录
            elButton.click();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭 WebDriver
            //driver.quit();
        }

    }
}
